﻿// ....................................................................................................................
//	Author: Copyright (C) 2012 Ing. Marek Hubal
//	Email: hubal@acasoftware.org
// ....................................................................................................................

using System;

namespace core
{
    /// <summary>Trieda IFileExplorerForm_Viewer</summary>
    public class IFileExplorerForm_Viewer : IForm
    {
        /// <summary>Trieda IViewerSettings</summary>
        public class IViewerSettings
        {
            public int Mode0_ColsCount; // Mod 0
            public int Mode0_Encoding; // Kodovanie pre mod 0 - 0 - DOS, 1 - ANSI
            public string Mode0_FontName;
            public float Mode0_FontSize;
            public int Mode0_FontStyle;
            public string Mode0_FindData;

            /// <summary>Konstruktor</summary>
            public IViewerSettings()
            {
                Mode0_ColsCount = 80;
                Mode0_Encoding = 1;
                Mode0_FontName = "Consolas";
                Mode0_FontSize = 11;
                Mode0_FontStyle = (int)System.Drawing.FontStyle.Regular;
                Mode0_FindData = "";
            }

            /// <summary>Serializacia obsahu</summary>
            /// <returns>serializovany obsah</returns>
            public string Serialize()
            {
                using (IParamList L = new IParamList())
                {
                    L.SetParam("M0_CC", Mode0_ColsCount);
                    L.SetParam("M0_ENC", Mode0_Encoding);
                    L.SetParam("M0_FN", Mode0_FontName);
                    L.SetParam("M0_FS", Mode0_FontSize);
                    L.SetParam("M0_FST", Mode0_FontStyle);
                    L.SetParam("M0_FD", Mode0_FindData);
                    return L.Serialize(true);
                }
            }
            /// <summary>Deserializacia obsahu</summary>
            /// <param name="pData">serializovany obsah</param>
            public void Deserialize(string pData)
            {
                using (IParamList L = new IParamList())
                {
                    L.Deserialize(pData, true);
                    Mode0_ColsCount = L.GetParam("M0_CC", Mode0_ColsCount);
                    Mode0_Encoding = L.GetParam("M0_ENC", Mode0_Encoding);
                    Mode0_FontName = L.GetParam("M0_FN", Mode0_FontName);
                    Mode0_FontSize = (float)L.GetParam("M0_FS", Mode0_FontSize);
                    Mode0_FontStyle = L.GetParam("M0_FST", Mode0_FontStyle);
                    Mode0_FindData = L.GetParam("M0_FD", Mode0_FindData);
                }
            }
        }

        /// <summary>Property: CanCloseForm</summary>
        public override bool CanCloseForm
        {
            get
            {
                switch (FViewMode)
                {
                    case 0: return !FBinaryViewer.IsBusy;
                    default: return true;
                }
            }
        }

        public System.Collections.Generic.List<string> FilesToView; // Zoznam suborov na prezretie
        protected int FViewMode; // Aktualny mod zobrazenia

        protected IViewerSettings FSettings;
        protected IBinaryTextViewer FBinaryViewer; // Objekt binarneho zobrazovaca

        /// <summary>Konstruktor</summary>
        public IFileExplorerForm_Viewer()
        {
            FViewMode = -1;
            FSettings = new IViewerSettings(); FSettings.Deserialize(IApp.Settings.FileExplorer_InternalViewerSettings);
            FilesToView = new System.Collections.Generic.List<string>();
        }
        /// <summary>Dispose metoda</summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            FBinaryViewer = (IBinaryTextViewer)ICore.DisposeObject(FBinaryViewer);
            base.Dispose(disposing); // Volam predchodcu
        }

        /// <summary>Metoda: OnCreateForm</summary>
        /// <returns>true / false</returns>
        protected override bool OnCreateForm()
        {
            if (!base.OnCreateForm()) return false; // Volam predchodcu

            // Inicializacia
            MinimizeBox = true; MaximizeBox = true; FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable; ShowIcon = true; ShowInTaskbar = true; StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

            SetBounds(0, 0, 780, 540); System.Drawing.Size CS = ClientSize;

            ChangeViewMode(0); // Nastavim binarny mod zobrazenia

            return true; // Vsetko OK
        }
        /// <summary>Zmena modu zobrazenia</summary>
        /// <param name="pMode">novy mod</param>
        protected void ChangeViewMode(int pMode, int pFileIndex = 0)
        {
            if (FViewMode == pMode) return; // Ziadna zmena?
            switch (FViewMode) // Zrusim aktualny mod
            {
                case 0: 
                {
                    FBinaryViewer.Dispose(); FBinaryViewer = null;
                } break;
            }
            
            FViewMode = pMode; // Ulozim
            Text = ""; // Zrusim nadpis okna

            switch (FViewMode) // Vytvorim novy mod
            {
                case 0:
                    {
                        FBinaryViewer = new IBinaryTextViewer(this, FSettings, FilesToView, pFileIndex); FBinaryViewer.Dock = System.Windows.Forms.DockStyle.Fill;
                        Controls.Add(FBinaryViewer);
                    } break;
            }
        }

        /// <summary>Metoda: OnClosing</summary>
        /// <param name="e">parameter</param>
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            switch (FViewMode) // Ukladam nastavenie
            {
                case 0:
                    {
                        FBinaryViewer.SaveSettings(FSettings);
                    } break;
            }
            IApp.Settings.FileExplorer_InternalViewerSettings = FSettings.Serialize();
            base.OnClosing(e);
        }

        /// <summary>Aktualizacia titulky okna</summary>
        /// <param name="pCurrentFileName">aktualny subor</param>
        /// <param name="pError">priznak chyby pri otvarani</param>
        public void UpdateTitle(string pCurrentFileName, bool pError = false)
        {
            Text = string.Format(pError ? lang.FORM_VIEWER_TEXT_1_1 : lang.FORM_VIEWER_TEXT_1, pCurrentFileName);
        }
    }
}
